﻿
@{
    ViewData["Title"] = "DifferenceUpdateIndex";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />


<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">盘点人</label>
        <div class="layui-input-block">
            <input type="text" id="stocker" name="stocker" lay-verify="required" placeholder="请输入盘点人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">盘点日期</label>
        <div class="layui-input-block">
            <input type="date" id="stockDate" name="stockDate" lay-verify="required" placeholder="请输入盘点日期" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">储位</label>
        <div class="layui-input-block">
            <input type="text" placeholder="" name="storage" id="storage" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">托盘</label>
        <div class="layui-input-block">
            <input type="text" name="tray" id="tray" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">商品编码</label>
        <div class="layui-input-block">
            <input type="text" id="commodityCode" name="commodityCode" lay-verify="required" placeholder="请输入商品编码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
            <input type="text" id="commodityName" name="commodityName" lay-verify="required" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">账目数量</label>
        <div class="layui-input-block">
            <input type="number" id="accounts" name="accounts" lay-verify="required|number" placeholder="请输入账目数量" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">盘点数量</label>
        <div class="layui-input-block">
            <input type="number" id="stockNum" name="stockNum" lay-verify="required|number" placeholder="请输入盘点数量" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">生产日期</label>
        <div class="layui-input-block">
            <input type="date" id="protructDate" name="protructDate" lay-verify="required" placeholder="请输入生产日期" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">货主</label>
        <div class="layui-input-block">
            <input type="text" id="owner" name="owner" lay-verify="required" placeholder="请输入货主" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">货主编码</label>
        <div class="layui-input-block">
            <input type="text" id="ownerCode" name="ownerCode" lay-verify="required" placeholder="请输入货主编码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单据状态</label>
        <div class="layui-input-block">
            <input type="radio" name="stockStatus" value="true" title="已完成" checked>
            <input type="radio" name="stockStatus" value="false" title="未完成">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
        </div>
    </div>
</form>

<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

<script>
    // 页面加载完成后执行的函数
    $(function () {
        mainUnitListOne();
        getMainUnitList();
    });

    // 获取主数据进行反填
    function mainUnitListOne() {
    
        $.ajax({
            url: '/Difference/GetDifferenceById',
            type: 'get',
            data: {
                id: @ViewBag.Id
            },
            dataType: 'json',
            success: function (res) {
                console.log(res.data);
                $("#stocker").val(res.data.stocker);
                $("#stockDate").val(formatDate(res.data.stockDate));
                $("#storage").val(res.data.storage);
                $("#tray").val(res.data.tray);
                $("#unit").val(res.data.unit)
                $("#commodityCode").val(res.data.commodityCode);
                $("#commodityName").val(res.data.commodityName);
                $("#accounts").val(res.data.accounts);
                $("#protructDate").val(res.data.protructDate);
                $("#stockNum").val(res.data.stockNum);
                $("#owner").val(res.data.owner);
                $("#ownerCode").val(res.data.ownerCode);


                // 设置 radio 选中状态
                $('input[name="stockStatus"][value="' + res.data.stockStatus + '"]').prop('checked', true);

                layui.form.render("select");
                layui.form.render("radio");
            }
        });
    }

    function formatDate(dateString) {
        if (!dateString) return '';
        const date = new Date(dateString);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    }

    // 获取主单位列表
    function getMainUnitList() {
        $.ajax({
            url: '/Difference/GetDifferenceById',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                console.log(res.data);
                if (res.code == 0) {
                    var select = $('#primaryUnit'); // 获取 select 元素

                    // 清空现有的选项（除了第一个默认选项）
                    select.find('option:not(:first)').remove();

                    // 动态添加选项
                    $.each(res.data, function (key, value) {
                        select.append('<option value="' + value.id + '">' + value.stocker + '</option>');
                    });
                    layui.form.render('select');
                }
            }
        });
    }

    layui.use(['form', 'laydate'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 监听是否启用的选择
        form.on('radio(stockStatus)', function (data) {
            var value = data.value; // 获取选中的值
            console.log(value);
            if (value === 'true') {
                // 如果是启用，隐藏额外的字段
                $('#mainUnitFields').hide();
            } else {
                // 如果不是启用，显示额外的字段
                $('#mainUnitFields').show();
            }
        });

        // 提交事件
        form.on('submit(demo1)', function (data) {
            console.log(data.field); // 获取表单字段值

            var field = data.field;
            field.id = @ViewBag.Id;
            field.stockStatus = field.stockStatus === 'true'; // 将字符串转换为布尔值
           
            console.log(field);
            $.ajax({
                url: '/Difference/UpdateDifference',
                type: 'post',
                data: field,
                dataType: 'json',
                success: function (res) {
                    if (res.code == true) {
                        layer.alert('修改成功！', function () {
                            closeWorkstation();
                        });
                    } else if (res.code == false) {
                        layer.alert('修改失败！,编号或名称已存在！');
                    }
                }
            });
            console.log(1111111);
            return false; // 阻止默认 form 跳转

        });
    });

    // 关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); // 先得到当前的索引
        parent.layer.close(index); // 再执行关闭
    }
</script>




